
using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace NotAClue.Web.UI.WebControls
{
    [ToolboxData("<{0}:EnhancedGridView runat=server></{0}:EnhancedGridView>")]
    [ToolboxBitmap("EnhancedGridView.bmp")]
    public class EnhancedGridView : GridView
    {
        [Bindable(true)]
        [Category("Appearance")]
        [TypeConverter(typeof(WebColorConverter))]
        [Description("Row Highlight Color")]
        public Color RowHighlightColor
        {
            get
            {
                object o = ViewState["RowHighlightColor"];
                if (o == null)
                {
                    return Color.Empty;
                }
                else
                {
                    return (Color)o;
                }
            }
            set
            {
                ViewState["RowHighlightColor"] = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue("")]
        [Description("Specifies the CommandName used in the server-side DataGrid event when the row is clicked.")]
        public string RowClickEventCommandName
        {
            get
            {
                object o = ViewState["RowClickEventCommandName"];
                if (o == null)
                {
                    return string.Empty;
                }
                else
                {
                    return (string)o;
                }
            }
            set
            {
                ViewState["RowClickEventCommandName"] = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(false)]
        [Description("Indicates whether or not rows are highlighted/clickable.")]
        public bool RowSelectionEnabled
        {
            get
            {
                object o = ViewState["RowSelectionEnabled"];
                if (o == null)
                {
                    return false;
                }
                else
                {
                    return (bool)o;
                }
            }
            set
            {
                ViewState["RowSelectionEnabled"] = value;
                if (RowHighlightColor == Color.Empty)
                {
                    RowHighlightColor = Color.White;
                }
            }
        }

        protected override GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
        {
            // Create the DataGridItem
            EnhancedGridViewRow row = new EnhancedGridViewRow(rowIndex, dataSourceIndex, rowType, rowState);

            //Set the client-side onmouseover and onmouseout if RowSelectionEnabled == true
            if (RowSelectionEnabled && rowType != DataControlRowType.Header && rowType != DataControlRowType.Footer && rowType != DataControlRowType.Pager)
            {
                row.Attributes["onmouseover"] = "javascript:NAC_ChangeBackColor(this, true, '#" + (RowHighlightColor.ToArgb()).ToString("X").Substring(2, 6) + "');this.style.cursor = 'hand';";
                row.Attributes["onmouseout"] = "javascript:NAC_ChangeBackColor(this, false, '');";
            }

            // return the EnhancedDataGridItem
            return row;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (RowSelectionEnabled)
            {
                // Get a ClientScriptManager reference from the Page class.
                ClientScriptManager csm = Page.ClientScript;

                //get the type
                Type scriptType = this.GetType();

                // Define the name and type of the client scripts on the page.
				string scriptName = "NotAClue.Web.UI.WebControls.ResourceFiles.NAC_RowHighLighter.js";

                // Check to see if the startup script is already registered.
                if (!csm.IsClientScriptBlockRegistered(scriptType, scriptName))
                {
                    // include main Flash Content script
                    string urlJS = csm.GetWebResourceUrl(scriptType, scriptName);
                    csm.RegisterClientScriptInclude(scriptName, urlJS);
                }

                // add the click client-side event handler, if needed
                if (RowClickEventCommandName != string.Empty && this.ChildControlsCreated && Controls.Count > 0)
                {
                    CreateClickEvent(csm);
                }
            }
            else
            {
                return;
            }
        }

        protected virtual void CreateClickEvent(ClientScriptManager csm)
        {
            foreach (EnhancedGridViewRow ROW in this.Rows)
            {
                if (ROW.RowType == DataControlRowType.DataRow)
                {
                    ROW.Attributes["onclick"] = csm.GetPostBackClientHyperlink(ROW, RowClickEventCommandName);
                }
            }
        }
    }

    public class EnhancedGridViewRow : GridViewRow, IPostBackEventHandler
    {
        public EnhancedGridViewRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState) : base(rowIndex, dataSourceIndex, rowType, rowState) { }

        public void RaisePostBackEvent(string eventArgument)
        {
            CommandEventArgs commandArgs = new CommandEventArgs(eventArgument, null);
            GridViewCommandEventArgs args = new GridViewCommandEventArgs(this, this, commandArgs);
            base.RaiseBubbleEvent(this, args);
        }
    }
}